/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.memory.criteria.criterion;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;
import com.genia.toolbox.persistence.basis.manager.AbstractPersistenceManager;
import com.genia.toolbox.persistence.criteria.enumeration.MatchMode;
import com.genia.toolbox.persistence.exception.AggregateException;
import com.genia.toolbox.persistence.exception.FieldDefinitionException;
import com.genia.toolbox.persistence.memory.criteria.MemoryCriteria;
import com.genia.toolbox.persistence.memory.criteria.field.AbstractMemoryField;

/**
 * This criterion represent an comparaison between two fields.
 */
public class MemoryFuzzyFieldComparator
    implements MemoryCriterion
{

  /**
   * the first operand.
   */
  private final transient AbstractMemoryField field1;

  /**
   * the second operand.
   */
  private final transient AbstractMemoryField field2;

  /**
   * the mode of matching for this field.
   */
  private final transient MatchMode matchMode;



  /**
   * constructor.
   * 
   * @param field1
   *          the first operand
   * @param field2
   *          the second operand
   * @param matchMode
   *          the mode of matching for this field
   */
  public MemoryFuzzyFieldComparator(final AbstractMemoryField field1, final AbstractMemoryField field2, final MatchMode matchMode)
  {
    this.field1 = field1;
    this.field2 = field2;
    this.matchMode = matchMode;
  }



  /**
   * return <code>true</code> if the <code>BasicCriteriaResult</code> given
   * in parameter match this <code>Criterion</code> in the
   * <code>Criteria> criteria</code>.
   * 
   * @param persistenceManager
   *          the implementation of <code>PersistenceManager</code>
   * @param criteria
   *          the criteria this <code>Criterion</code> comes from
   * @param line
   *          the line to test
   * @return if the <code>BasicCriteriaResult</code> given in parameter match
   *         this <code>Criterion</code>.
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   */
  public boolean match(final AbstractPersistenceManager persistenceManager, final MemoryCriteria<?> criteria, final BasicCriteriaResult<?> line)
      throws FieldDefinitionException, AggregateException
  {
    final Object object1 = field1.getSimpleValue(line);
    final Object object2 = field2.getSimpleValue(line);
    if (object1 == null || object2 == null) {
      return false;
    }
    final String value1 = object1.toString();
    final String value2 = object2.toString();
    if (!value1.contains(value2)) {
      return false;
    }
    if (matchMode.startsWith() && !value1.startsWith(value2)) {
      return false;
    }
    if (matchMode.endsWith() && !value1.endsWith(value2)) {
      return false;
    }
    return true;
  }



  /**
   * Creates and returns a deep copy of this object.
   * 
   * @return a deep copy of this object
   */
  @Override
  public MemoryFuzzyFieldComparator clone()
  {
    return this;
  }
}
